package cn.edu.hyit.zhao.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 * 
 * @author Neko
 * @version 1.0
 */
public class Md5Utils {
	/**
	 * @param str
	 * @return
	 * @date 2016-01-12
	 * @description 32位小写MD5加密
	 */
	public static String parseStrToMd5L32(String str) {
		String resultStr = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] bytes = md5.digest(str.getBytes());
			StringBuffer stringBuffer = new StringBuffer();
			for (byte b : bytes) {
				int bt = b & 0xff;
				if (bt < 16) {
					stringBuffer.append(0);
				}
				stringBuffer.append(Integer.toHexString(bt));
			}
			resultStr = stringBuffer.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return resultStr;
	}

	/**
	 * @param str
	 * @return
	 * @date 2016-01-12
	 * @description 32位大写MD5加密
	 */
	public static String parseStrToMd5U32(String str) {
		String resultStr = parseStrToMd5L32(str);
		if (resultStr != null) {
			resultStr = resultStr.toUpperCase();
		}
		return resultStr;
	}

	/**
	 * @param str
	 * @return
	 * @date 2016-01-12
	 * @description 16位小写MD5加密
	 */
	public static String parseStrToMd5L16(String str) {
		String resultStr = parseStrToMd5L32(str);
		if (resultStr != null) {
			resultStr = resultStr.substring(8, 24);
		}
		return resultStr;
	}

	/**
	 * @param str
	 * @return
	 * @date 2016-01-12
	 * @description 16位大写MD5加密
	 */
	public static String parseStrToMd5U16(String str) {
		String resultStr = parseStrToMd5L16(str);
		if (resultStr != null) {
			resultStr = resultStr.toUpperCase();
		}
		return resultStr;
	}

	public static void main(String[] args){
	    System.out.println(parseStrToMd5L32("123456"));
	}
}
